﻿using System;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;

using TrivialPurfuit.Data;
using TrivialPurfuit.Business;

namespace TrivialPurfuit.Presentation.StartGame
{
    class StartGameViewModel : BindableObject
    {
        private ObservableCollection<QuestionCategory> categories;

        public QuestionCategory Category1
        {
            get
            {
                return GameBoard.Instance.Category1;
            }

            set
            {
                GameBoard.Instance.Category1 = value;
            }
        }

        public QuestionCategory Category2
        {
            get
            {
                return GameBoard.Instance.Category2;
            }

            set
            {
                GameBoard.Instance.Category2 = value;
            }
        }

        public QuestionCategory Category3
        {
            get
            {
                return GameBoard.Instance.Category3;
            }

            set
            {
                GameBoard.Instance.Category3 = value;
            }
        }

        public QuestionCategory Category4
        {
            get
            {
                return GameBoard.Instance.Category4;
            }

            set
            {
                GameBoard.Instance.Category4 = value;
            }
        }
        
        public bool HaveAtleastTwoPlayers
        {
            get
            {
                if (GameBoard.Instance.Pieces.Count >= 2)
                    return true;
                else
                    return false;
            }
        }

        public ListCollectionView View1 { get; private set; }
        public ListCollectionView View2 { get; private set; }
        public ListCollectionView View3 { get; private set; }
        public ListCollectionView View4 { get; private set; }

        private bool filter(object item)
        {
            QuestionCategory category = (QuestionCategory)item;
            bool keepItem = true; // Assume the item will be shown in the list

            if (category.ID == CategoryCON.NULL.ID)
                keepItem = false;
            
            return keepItem;
        }

        public string Player1Name
        {
            get
            {
                return GameBoard.Instance.Piece1.PlayerName;
            }
            set
            {
                GameBoard.Instance.Piece1.PlayerName = value;
                GameBoard.Instance.determineWhichPiecesAreUsed();
                this.RaisePropertyChanged("HaveAtleastTwoPlayers");
            }
        }

        public string Player2Name
        {
            get
            {
                return GameBoard.Instance.Piece2.PlayerName;
            }
            set
            {
                GameBoard.Instance.Piece2.PlayerName = value;
                GameBoard.Instance.determineWhichPiecesAreUsed();
                this.RaisePropertyChanged("HaveAtleastTwoPlayers");
            }
        }

        public string Player3Name
        {
            get
            {
                return GameBoard.Instance.Piece3.PlayerName;
            }
            set
            {
                GameBoard.Instance.Piece3.PlayerName = value;
                GameBoard.Instance.determineWhichPiecesAreUsed();
                this.RaisePropertyChanged("HaveAtleastTwoPlayers");
            }
        }

        public string Player4Name
        {
            get
            {
                return GameBoard.Instance.Piece4.PlayerName;
            }
            set
            {
                GameBoard.Instance.Piece4.PlayerName = value;
                GameBoard.Instance.determineWhichPiecesAreUsed();
                this.RaisePropertyChanged("HaveAtleastTwoPlayers");
            }
        }

        public StartGameViewModel()
        {
            this.categories = QuestionCollection.Categories;

            SortDescription nameSort = new SortDescription("Name", ListSortDirection.Ascending);

            View1 = new ListCollectionView(categories);
            View1.SortDescriptions.Add(nameSort);
            View1.Filter = new Predicate<Object>(filter);
            View1.MoveCurrentTo(categories[1]);

            View2 = new ListCollectionView(categories);
            View2.SortDescriptions.Add(nameSort);
            View2.Filter = new Predicate<Object>(filter);
            View2.MoveCurrentTo(categories[2]);

            View3 = new ListCollectionView(categories);
            View3.SortDescriptions.Add(nameSort);
            View3.Filter = new Predicate<Object>(filter);
            View3.MoveCurrentTo(categories[3]);

            View4 = new ListCollectionView(categories);
            View4.SortDescriptions.Add(nameSort);
            View4.Filter = new Predicate<Object>(filter);
            View4.MoveCurrentTo(categories[4]);
        }

    }
}
